package com.WaVel.controller.admin;

import com.Emplyment.result.Result;
import com.Emplyment.vo.ResumeTemplateDownAndPreviewVo;
import com.Emplyment.vo.ResumeTemplateVo;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.itWaVel.feign.client.TemplateClient;
import com.WaVel.service.ResumeTemplateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

@RestController
@RequestMapping("/template")
@Slf4j
public class TemplateServerController {

    @Autowired
    private ResumeTemplateService resumeTemplateService;

    @Autowired
    private TemplateClient templateClient;

    //查询简历模板
    @GetMapping("/getResumeTemplate")
    @SentinelResource("Template")
    public Result<List<ResumeTemplateVo>> getResumeTemplate() {
        log.info("获取简历模板");
        return templateClient.getResumeTemplate();
    }

//    private static final String FILE_DIRECTORY = "D:/就业/后端实习生.pdf"; // 本地文件目录
    @GetMapping("/preview")
    public ResponseEntity<Resource> previewFile(@RequestParam Integer templateId) throws IOException {
        // 构建文件路径
        log.info("文件Id：{}",templateId);
        ResumeTemplateDownAndPreviewVo resumeTemplateDownAndPreviewVo = resumeTemplateService.ByIdGetResumeTemplate(templateId);
        byte[] file = resumeTemplateDownAndPreviewVo.getPreviewFile();
        // 转换为Resource
        Resource resource = new ByteArrayResource(file);
        String fileName = resumeTemplateDownAndPreviewVo.getName();

        // 设置响应头
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=\"" + fileName + "\"");
        headers.add(HttpHeaders.CONTENT_TYPE, "application/octet-stream");

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(resource.contentLength())
                .body(resource);
    }

    //删除简历模板
    @DeleteMapping("/deleteTemplate/{templateId}")
    public Result deleteTemplate(@PathVariable("templateId") Integer templateId) {
        log.info("删除简历模板id：{}",templateId);
        return resumeTemplateService.deleteTemplate(templateId);
    }


    //上传简历模板
    @PostMapping("/upload")
    public Result uploadFile(@RequestParam("previewFile") MultipartFile previewFile,
                             @RequestParam("downloadFile") MultipartFile downloadFile,
                             @RequestParam("imageUrl") String imageUrl,
                             @RequestParam("name") String name) {
        log.info("上传简历模板名称：{}",name);
        if (previewFile.isEmpty() || downloadFile.isEmpty() || imageUrl.isEmpty() || name.isEmpty()){
            return Result.error("文件不能为空");
        }
        resumeTemplateService.uploadFile(previewFile,downloadFile,imageUrl,name);
        return Result.success();
    }
}
